var scService = angular.module('scService', []);
scService.factory('commonService', function($q,$http,$httpParamSerializerJQLike) {
	var factory = {};
	factory.commonAjaxByGet = function(url) {
		var deferred = $q.defer();
		$http.get("/appserver" + url).success(function(data, status, headers, config) { 
			deferred.resolve(data);
		}).error(function(data, status, headers, config) {});
		return deferred.promise;
	};
	factory.commonAjaxByPost = function(url, postData) {
		var deferred = $q.defer();
		$http({
			method: 'POST',
			data: $httpParamSerializerJQLike(postData),
			url: "/appserver" + url,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			cache: false
		}).success(function(data, status, headers, config) {
			deferred.resolve(data);
		}).error(function(data,status,headers,config){
            deferred.reject(data);
        });
		return deferred.promise;
	};
	factory.commonAjaxByPut = function(url, postData) {
		var deferred = $q.defer();
		$http({
			method: 'PUT',
			data: $httpParamSerializerJQLike(postData),
			url: "/appserver" + url,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			cache: false
		}).success(function(data, status, headers, config) {
			deferred.resolve(data);
		});
		return deferred.promise;
	};
	return factory;

});
scService.service('jsonToStr',function(){
    this.transform = function(jsonData){
        var string = '';
        
        for(str in jsonData){
            string = string + str +'=' + jsonData[str] +'&';
        }
        var _last = string.lastIndexOf('&');
        
        string = string.substring(0,_last);
        
        return string;
    };
});
// 扫码部分
scService.service('scCodeService', function(commonService) {
	return {
		//发送验证码
		sendVerifyCode:function(postData){
			var url = "/pub/sendVerifyCode?phone="+postData.phone+"&merchantCode="+postData.merchantCode;
			return commonService.commonAjaxByGet(url);
		},
		//获取openId
		getOpenId:function(postData){
			 var url = "/pub/getAccessToken?source="+postData.source+"&merchantCode="+postData.merchantCode+"&code="+postData.code;
			return commonService.commonAjaxByGet(url);
		},
		// 获取userCard
		getUserCard:function(postData){
			var url = "/user/getUserCard?userId="+postData.userId;
			return commonService.commonAjaxByGet(url);
		},
		//登录（验证验证码）
		login:function(postData){
			var url = "/pub/validationVerifyCode";
			return commonService.commonAjaxByPost(url,postData);
		},
		//自动登录
		autoLogin:function(postData){
			var url = "/user/autoLogin";
			return commonService.commonAjaxByPost(url,postData);
		},
		//退出登录
		unlockUser:function(postData){
			var url = "/station/unlockUser";
			return commonService.commonAjaxByPost(url,postData);
		},
		//获取站点详情
		getStationDetail:function(postData){
			var url = "/user/getStationDetail";
			return commonService.commonAjaxByPost(url,postData);
		},
		//查询充电桩详情
		getChargerDetailByQrCode: function(postData){
			var url = "/charger/getChargerDetailByQrCode?qrCode=" + postData.qrCode;
			return commonService.commonAjaxByGet(url);
		},
		//开启充电
		openCharge:function(postData){
			var url = "/charge/openCharge";
			return commonService.commonAjaxByPost(url,postData);
		},
		//结束充电
		closeCharge:function(postData){
			var url = "/charge/closeCharge";
			return commonService.commonAjaxByPut(url,postData);
		},
		//获取费率
		getRate:function(postData){
			var url = "/charger/getRate?chargerId="+postData.chargerId;
			return commonService.commonAjaxByGet(url);
		},
		//枪口充电实时状态
		getCurrentChargerInfo:function(postData){
			var url = "/getCurrentChargerInfo?qr_code="+postData.qrCode+"&plug_id="+postData.plugNo;
			return commonService.commonAjaxByGet(url);
		},
		//枪口状态
		plugStatus:function(postData){
			var url = "/charger/plugStatus?plugNo="+postData.plugNo+"&chargerId="+postData.chargerId;
			return commonService.commonAjaxByGet(url);
		},
		//获取正在进行的业务操作
		getOngoingOperation:function(postData){
			var url = "/charge/getOngoingOperation?userCard="+postData.userCard;
			return commonService.commonAjaxByGet(url);
		},
		//查询订单
		getOrderList:function(postData) {
			var url = "/fund/getFundList?userId="+postData.userId+"&pageNo="+postData.pageNo;
			return commonService.commonAjaxByGet(url);
		},
		//查询订单详细
		getOrderDetail:function(postData) {
			var url = "/fund/fundDetail?orderNo="+postData.orderNo;
			return commonService.commonAjaxByGet(url);
		},
		//获取支付参数
		getPayParams:function(postData) {
			var url = "/fund/generateOrders";
			return commonService.commonAjaxByPost(url,postData);
		},
		//調用微信签名
		getJsSign:function(postData){
			var url = "/pub/getJsSign?source="+mainConfig.source+"&merchantCode="+mainConfig.merchantCode+"&signType=sha1&url=" + window.location.href;
			return commonService.commonAjaxByGet(url);
		},
		//判断是否有支付订单
		getFundList:function(postData){
			var url = "/fund/getFundList?userId="+postData.userId+"&pageNo=1&status=0";
			return commonService.commonAjaxByGet(url);
		},
		//预约
		startReservation:function (postData) {
			var url = "/reservation/reservation";
			return commonService.commonAjaxByPost(url,postData);
		},
		//取消预约
		cancelReservation:function(postData) {
			var url = "/reservation/cancelReservation?reservationSn="+postData.reservationSn;
			return commonService.commonAjaxByGet(url);
		},
		//获取城市列表
		getCityList:function(argument) {
			var url = "/pub/getCityList";
			return commonService.commonAjaxByGet(url);
		}
	};
})

